'''
Created on Dec 7, 2015

@author: JimmyMo
'''
import xml.etree.ElementTree as ET

import logging
import logging.config
logging.config.fileConfig('/home/JimmyMo/git/resourcecare/resourcecare/res/logging.conf')
logger = logging.getLogger('simpleExample')
# from resourcecare.locator.locator import Locator


# logging.basicConfig(filename='logging.log',level=logging.DEBUG)

class ConfigNode(object):
    def __init__(self, enabled, name):
        self.enabled = enabled
        self.name = name;
        
class LocatorConfig(ConfigNode):
    def __init__(self, enabled, name, locationPath, includeType):
        super().__init__(enabled, name)
        self.locationPath = locationPath
        self.includeType = includeType
    def __str__(self, *args, **kwargs):
        return repr(self) + " enabled: %s, name: %s, location: %s, include: %s" % (self.enabled, self.name, self.locationPath, self.includeType)
        

class ConfigInfo(object):
    def __init__(self, configPath):
        self.configPath = configPath
    
    def parse(self):
        root = parseRoot(self.configPath)
        self.locatorConfigs = parseLocations(root)
        
        
def parseRoot(configPath):
    logging.info("config file is {}".format(configPath))
    tree = ET.parse(configPath)
    
    root = tree.getroot()
    return root


def parseLocations(root):
    locatorXmlElements = root.findall(".//Locations/Location")
    locatorConfigs = []
#     locatorConfigs = [LocatorConfig() for lc in locationConfigs]
    for lc in locatorXmlElements:
        enabled = bool(lc.get('enabled', 'true'))
        name = lc.attrib['name']
        locationPath = lc.attrib['locationPath']
        includeType = lc.get('includeType', '*')
        locatorConfig = LocatorConfig(enabled, name, locationPath, includeType)
        locatorConfigs.append(locatorConfig)
#         res_location = Locator(enabled, name, locationPath, includeType)
#         self.res_locations[name] = res_location
#         logging.debug(res_location)
    return locatorConfigs

# def parseFormatters(self):
#     formatterConfigs = self.root.findall(".//Formatters/Formatter")
#     for fc in formatterConfigs:
#         enabled = bool(fc.get('enabled', 'true'))
#         name = fc.attrib['name']
# 
# if __name__ == "__main__":
#     pass

# class Configuration(object):
#     '''
#     classdocs
#     '''
# 
#     def __init__(self, configPath):
#         '''
#         Constructor
#         '''
#         self.configPath = configPath
#         self.res_locations = {};
#         
#     def parse(self):
#         
#         logging.info("config file is {}".format(self.configPath))
#         tree = ET.parse(self.configPath)
#         
#         self.root = tree.getroot()
#         self.locations = self.parseLocations()
# 
#     def parseLocations(self):
#         locationConfigs = self.root.findall(".//Locations/Location")
# #         logging.debug(locationConfigs[0].attrib)
# #         logging.debug(locationConfigs[0].attrib['name'])
#         for lc in locationConfigs:
# #             Configuration.assignConfigNode(lc, 'enabled', 'name', 'path', 'include');
#             enabled = bool(lc.get('enabled', 'true'))
#             name = lc.attrib['name']
#             locationPath = lc.attrib['path']
#             includeType = lc.get('include', '*')
#             res_location = Locator(enabled, name, locationPath, includeType)
#             self.res_locations[name] = res_location
#             logging.debug(res_location)
# 
#     def parseFormatters(self):
#         formatterConfigs = self.root.findall(".//Formatters/Formatter")
#         for fc in formatterConfigs:
#             enabled = bool(fc.get('enabled', 'true'))
#             name = fc.attrib['name']
# #             extractor = 
#       
